﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace WinformDevFramework.Core
{
    public static class ExtensionMethod
    {
        //将model值赋值给控件
        public static void ModelDataToControl(this Control con, object data)
        {
            Type type = data.GetType();
            foreach (PropertyInfo pro in type.GetProperties())
            {
                if (pro.IsDefined(typeof(ModelBindControlAttribute), true))
                {
                    Control control = getControl(con, pro.GetCustomAttribute<ModelBindControlAttribute>().GetModelName());
                    if (control != null)
                    {

                        if (control is TextBox)
                        {
                            TextBox txt = (TextBox)control;
                            txt.Text = pro.GetValue(data).ToString();
                        }
                        else if (control is DateTimePicker)
                        {
                            DateTimePicker txt = (DateTimePicker)control;
                            txt.Text = pro.GetValue(data).ToString();
                        }
                        else if (control is CheckBox)
                        {
                            CheckBox txt = (CheckBox)control;
                            txt.Checked = Boolean.Parse(pro.GetValue(data).ToString());

                        }
                        else
                        {
                            MessageBox.Show($"控件{control.Name}找不到匹配项");
                        }
                    }
                }
            }
        }
        //将控件值赋值给model
        public static object ControlDataToModel(this Control con, object data)
        {
            Type type = data.GetType();
            object t = Activator.CreateInstance(type);
            foreach (PropertyInfo pro in type.GetProperties())
            {
                if (pro.IsDefined(typeof(ModelBindControlAttribute), true))
                {
                    Control control = getControl(con, pro.GetCustomAttribute<ModelBindControlAttribute>().GetModelName());
                    if (control != null)
                    {
                        if (control is TextBox)
                        {
                            TextBox txt = (TextBox)control;
                            pro.SetValue(t, txt.Text);
                        }
                        else if (control is DateTimePicker)
                        {
                            DateTimePicker txt = (DateTimePicker)control;
                            pro.SetValue(t, DateTime.Parse(txt.Text));
                        }
                        else if (control is CheckBox)
                        {
                            CheckBox txt = (CheckBox)control;
                            pro.SetValue(t, txt.Checked);
                        }
                       
                        else
                        {
                            MessageBox.Show($"属性{pro.Name}找不到匹配项");
                        }
                    }
                }
            }
            return t;
        }
        public static Control getControl(this Control con, string name)
        {
            Control[] controls = con.Controls.Find(name, true);
            if (controls.Length > 0)
                return (controls[0]);
            else
            {
                return null;
            }
        }
    }
}
